package com.example.test_client;

import com.example.test_client.MyApplication;
import java.io.*;
import java.net.*;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.*;

import android.util.Log;

public class MainActivity extends Activity implements android.view.View.OnClickListener {
	
	
	Button btnLogin;
	EditText txtUserName;
	EditText txtPassword;
	
	EditText txtMessage;
	
	int command;
	int execute;
	String message;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_screen);
        
        connectToServer();
        

        btnLogin=(Button) findViewById(R.id.btnLogin);
        txtUserName=(EditText) findViewById(R.id.txtUserName);
        txtPassword=(EditText) findViewById(R.id.txtPassword);
        Log.d("MAINFILE", "START");
        
        execute=0;
        command=0;
      
       

    }
    
    public void connectToServer(){
    	MyApplication.client.connect("192.168.14.213", 1111);
    }
    
    public void sendMessageToServer(String message){
    	MyApplication.client.sendMessage(message);
    }
    
    public void onClick(View v) {
    	final int id = v.getId();
        switch (id) {
        case R.id.btnLogin:
              execute=1;
            Log.d("MAINFILE", "Command::: " +command + "Execute:::"+ execute);
        	//t.start();
            socketThread();
            break;
            
        }
        
        
        	
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void socketThread(){
    	 Thread t = new Thread(new Runnable () {

         	
         	
 			public void run() {

 				if(execute==1){
 						sendMessageToServer("0 "+txtUserName.getText().toString()+" "+txtPassword.getText().toString());
 					execute=0;
 				}
 				runOnUiThread(new Runnable () {

 					public void run() {
 						
 					} 
 					
 				});
 			} 
 		//	}
         	
         });
         
        t.start();
    }

}
